<?php

namespace App\Http\Controllers\Wechat;

use App\Model\Rule;
use App\Model\WxImage;
use App\Model\WxReply;
use App\Server\WechatServer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WxImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        dd(11);
        $data=WxReply::where('type','image')->get();
//        dd($data);
        return view('wechat.wximage.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(WechatServer $wechatServer)
    {
      $model=  $wechatServer->ruleKey();
        return view('wechat.wximage.create',compact('model'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,WechatServer $wechatServer)
    {
        \Validator::make($request->all(), [
            'content' => 'required',
        ], [
            'content.required' => '回复内容为空'
        ])->validate();
//        dd($request->all());
//        dd(11);
            $rul=$wechatServer->storeKey($request->all());
//            dd($rul);
            $rul->wxReply()->create(['content' => $request['content'],'type'=>'image']);
//            dd(11);
        return redirect()->route('wechat.wxImage.index')->with('success','添加成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\WxImage  $wxImage
     * @return \Illuminate\Http\Response
     */
    public function show(WxImage $wxImage)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\WxImage  $wxImage
     * @return \Illuminate\Http\Response
     */
    public function edit(WxReply $wxImage, WechatServer $wechatServer)
    {
//        dd($wxImage);
        $model=  $wechatServer->ruleKey($wxImage['rule_id']);
        return view('wechat.wximage.edit',compact('model','wxImage'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\WxImage  $wxImage
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,WxReply $wxImage,WechatServer $wechatServer)
    {
//        dd($request->toArray());
        \Validator::make($request->all(), [
            'content' => 'required',
        ], [
            'content.required' => '回复内容为空'
        ])->validate();
//        $wechatServer->storeKey($request->all());
        $rul = $wechatServer->storeKey($request->all());
        $rul->wxReply()->update(['content' => $request['content']]);
        return redirect()->route('wechat.wxImage.index')->with('success','编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\WxImage  $wxImage
     * @return \Illuminate\Http\Response
     */
    public function destroy(WxReply $wxImage)
    {
        $wxImage->delete();
        return back()->with('success','删除成功');
    }

}
